package cn.jdy.plugins.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.admin.export.EvaluationzhliveExcelView;
import cn.jdy.plugins.admin.export.RedPacketExcelView;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.RedPacket;
import cn.jdy.plugins.service.RedPacketService;
import cn.jdy.plugins.service.LiveService;

/**
 * @title  聊天管理
 * @author jg
 * @since  2017-8-31
 */

@Controller
@RequestMapping("/redPacket")
public class RedPacketCtrl extends BaseCtrl{
	@Autowired
	private RedPacketService redPacketService;
	@Autowired
	private LiveService liveService;
	//默认列表路径

	
	/**
	 * 初始化
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required = false) Integer s,RedPacket chat) {
		ModelAndView mv = new ModelAndView("/redPacket/redPacketList.jsp");
		if (s != null)
			switch (s) {
			case 1:
				mv.addObject("msg", "保存成功！");
				break;
			case 2:
				mv.addObject("msg", "更新成功！");
				break;
			case 3:
				mv.addObject("msg", "删除成功！");
				break;
			case 4:
				mv.addObject("msg", "操作失败！");
				break;
			default:
				break;
			}
		
		mv.addObject("live", liveService.find(chat.getLiveId()));
		mv.addObject("zs", redPacketService.countZje(chat.getLiveId(),chat.getRpType()));
		mv.addObject("chat", chat);
		return mv;
	}
	
	/**
	 * 异步获取直播间列表
	 * @return
	 */
	@RequestMapping("/queryAllLive")
	@ResponseBody
	public Map<String, Object> queryAllLive(String lid){
	    Map<String, Object> jsonMap = new HashMap<String, Object>();
	    try {
	    if(!StringUtil.isNullOrEmpty(lid)){
		      jsonMap.put("dataList", liveService.find("id",Long.valueOf(lid)));

	    }else{
		      jsonMap.put("dataList", liveService.findAll());

	    }
	    } catch(Exception e) {
	      BaseLog.e(this.getClass(), "查询异常", e);
	    }
	    return jsonMap;
	}
	
	/**
	 * 异步查询
	 * 
	 * @param Live
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(RedPacket chat,String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", redPacketService.query(chat,fromDate,toDate));
			jsonMap.put("iTotalDisplayRecords", redPacketService.count(chat,fromDate,toDate));
			jsonMap.put("iTotalRecords", redPacketService.count(chat,fromDate,toDate));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	

	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public Map<String, Object> del(@PathVariable Long id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			this.redPacketService.delete(id);
			jsonMap.put("msg", "成功");

			// 清理文章缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			jsonMap.put("dataList", "失败");
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return jsonMap;
	}

	
	/**
	 * 根据记录和红包状态导出记录
	 * @param evaluationRecord
	 * @param fromDate
	 * @param toDate
	 * @param model
	 * @return
	 */
	@RequestMapping("/redPacketQueryByPage")
	public ModelAndView redPacketQueryByPage(RedPacket rpt,String fromDate, String toDate,ModelMap model) {
	    List<RedPacket> list = new ArrayList<RedPacket>();

		if(!("3").equals(rpt.getRpType())&&!("4").equals(rpt.getRpType())){
		
			DetachedCriteria dc = DetachedCriteria.forClass(RedPacket.class);
			dc.add(Restrictions.eq("liveId",rpt.getLiveId()));
			dc.add(Restrictions.eq("sendStatus","1"));
			dc.add(Restrictions.or(Restrictions.eq("rpType", "1"),Restrictions.eq("rpType", "2")));

			  list =redPacketService.find(dc);
		}else{
		  list = redPacketService.find("liveId",rpt.getLiveId(),"rpType",rpt.getRpType(),"sendStatus","1");

		}
	    model.put("dataSet", list);
	    model.put("rpType", rpt.getRpType());
	    model.put("searchDate", fromDate+"-"+toDate);
	    RedPacketExcelView evaluationRecordExcelView = new RedPacketExcelView();
	    return new ModelAndView(evaluationRecordExcelView, model);
	}
  
	  
}
